package cn.edu.neusoft.demo.hospital2.Controller;

import cn.edu.neusoft.demo.hospital2.Service.EmailService;
import cn.edu.neusoft.demo.hospital2.pojo.ProblemFeedback;
import cn.edu.neusoft.demo.hospital2.Service.ProblemFeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/api/feedback")
public class ProblemFeedbackController {

    @Autowired
    private ProblemFeedbackService problemFeedbackService;

    @PostMapping("/submit")
    public Map<String, Object> submitFeedback(@RequestBody ProblemFeedback problemFeedback) {
        Map<String, Object> result = new HashMap<>();
        boolean success = problemFeedbackService.submitFeedback(problemFeedback);
        if (success) {
            result.put("code", 200);
            result.put("message", "反馈提交成功");
        } else {
            result.put("code", 500);
            result.put("message", "反馈提交失败");
        }
        return result;
    }

    @GetMapping("/all")
    public Map<String, Object> getAllFeedbacks() {
        Map<String, Object> result = new HashMap<>();
        List<ProblemFeedback> list = problemFeedbackService.getAllFeedbacks();
        result.put("code", 200);
        result.put("data", list);
        result.put("message", "查询成功");
        return result;
    }

    @GetMapping("/listByUserId")
    public Map<String, Object> getFeedbacksByUserId(@RequestParam Integer userId) {
        Map<String, Object> result = new HashMap<>();
        List<ProblemFeedback> list = problemFeedbackService.getFeedbacksByUserId(userId);
        result.put("code", 200);
        result.put("data", list);
        result.put("message", "查询成功");
        return result;
    }

    @GetMapping("/{feedbackId}")
    public Map<String, Object> getFeedbackById(@PathVariable Integer feedbackId) {
        Map<String, Object> result = new HashMap<>();
        ProblemFeedback feedback = problemFeedbackService.getFeedbackById(feedbackId);
        result.put("code", 200);
        result.put("data", feedback);
        result.put("message", "查询成功");
        return result;
    }

    @PostMapping("/process")
    public Map<String, Object> processFeedback(@RequestBody ProblemFeedback problemFeedback) {
        Map<String, Object> result = new HashMap<>();
        boolean success = problemFeedbackService.processFeedback(problemFeedback);
        if (success) {
            result.put("code", 200);
            result.put("message", "反馈处理成功");
        } else {
            result.put("code", 500);
            result.put("message", "反馈处理失败");
        }
        return result;
    }

    @PutMapping("/update")
    public Map<String, Object> updateFeedback(@RequestBody ProblemFeedback problemFeedback) {
        Map<String, Object> result = new HashMap<>();
        boolean success = problemFeedbackService.updateFeedback(problemFeedback);
        if (success) {
            result.put("code", 200);
            result.put("message", "反馈更新成功");
        } else {
            result.put("code", 500);
            result.put("message", "反馈更新失败");
        }
        return result;
    }

    @DeleteMapping("/delete/{feedbackId}")
    public Map<String, Object> deleteFeedback(@PathVariable Integer feedbackId) {
        Map<String, Object> result = new HashMap<>();
        boolean success = problemFeedbackService.deleteFeedback(feedbackId);
        if (success) {
            result.put("code", 200);
            result.put("message", "反馈删除成功");
        } else {
            result.put("code", 500);
            result.put("message", "反馈删除失败");
        }
        return result;
    }
    // 在ProblemFeedbackController中新增方法
    @PostMapping("/submitByEmail")
    public Map<String, Object> submitFeedbackByEmail(@RequestBody Map<String, String> request) {
        Map<String, Object> result = new HashMap<>();
        try {
            emailService.sendFeedbackEmail(request.get("email"), request.get("content"));
            result.put("code", 200);
            result.put("message", "反馈邮件发送成功");
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "邮件发送失败");
        }
        return result;
    }

    // 需要新增的依赖注入
    @Autowired
    private EmailService emailService;
}